<?php

$request = isset($_GET['course_id']) ? $_GET['course_id'] : null;
$lecture = isset($_GET['lecture_id']) ? $_GET['lecture_id'] : null;
if ($request == null) {
	echo getMainPage();
} else {
	if ($lecture != null) {
		echo getLecturePage($request,$lecture);
	} else {
		echo getCoursePage($request);
	}
}

function getSemesterCourses($semester, $year) {
	global $woodb;
	$content = "";
	$courses = $woodb -> getCourses($year, $semester);
	if (!empty($courses)) {
		$content .= "<div><div class='woo_semester_title'>";
		if ($semester == "fs") {
			$content .= "Fall Semester $year";
		} else {
			$content .= "Spring Semester $year";
		}
		$content .= "</div><ul class='woo_courses'>";
		foreach ($courses as $e) {
			$content .= getShortCourseInfo($e);
		}
		$content .= "</ul></div>";
	}
	return $content;
}

function getCourseInfo($e) {
	$content .= "
				<li id='course_$e->course_id'>
					<div class='woo_course_name'>
						$e->name
					</div>
					<div class='woo_course_desc'>
						".stripslashes($e->description)."
					</div>
				</li>";
	return $content;
}

function getShortCourseInfo($e) {
	$content .= "
				<li id='course_$e->course_id'>
					<div class='woo_course_name'>
						$e->name
					</div>
					<div class='woo_course_desc'>
						".substr(stripslashes($e->description), 0, 215)."...
					</div>
				</li>";
	return $content;
}

function getMainPage() {
	$current_year = date("Y");

	$content = getSemesterCourses("fs", $current_year);
	$content .= getSemesterCourses("ss", $current_year);

	return $content;
}

function getCoursePage($course_id) {
	global $woodb;
	$courseInfo = $woodb -> getCourseByID($course_id);
	global $wp_query;
	$page_link = '?page_id='.$wp_query->post->ID;
	$course_link = $page_link."&course_id=".$courseInfo[0]->course_id;
	$content = "<div class='woo_breadcrumbs'>
	<a href='".$page_link."'>Courses<a> &raquo; <a href='".$course_link."'>".$courseInfo[0]->name."</a></div>";
	$content .= "<div class='woo_coursecalendar'><ul class='woo_lectures_course'>";
	$content .= getCourseInfo($courseInfo[0])."</ul>";
	$content .= "<div id='testcalendar'></div><div style='clear:both'></div></div>";
	$lectures = $woodb -> getLectures($course_id);
	if (isset($lectures)) {
		$content .= "<ul class='woo_lectures'>";
		foreach ($lectures as $l) {
			$content .= getLecture($l,$course_id);
		}
		$content .= "</ul>";
	}
	
	return $content;
}

function getLecturePage($course_id, $lecture_id) {
	global $woodb;
	$courseInfo = $woodb -> getCourseByID($course_id);
	$lectures = $woodb -> getLecture($lecture_id);

	global $wp_query;
	$page_link = '?page_id='.$wp_query->post->ID;
	$course_link = $page_link."&course_id=".$course_id;
	$lecture_link = $course_link."&lecture_id=".$lectures[0]->lecture_id;
	$content = "<div class='woo_breadcrumbs'>
	<a href='".$page_link."'>Courses<a> &raquo; 
	<a href='".$course_link."'>".$courseInfo[0]->name."</a> &raquo; 
	<a href='".$lecture_link."'>".$lectures[0]->name."</a></div>";
	$content .= "<ul class='woo_lectures'>";
		foreach ($lectures as $l) {
			$content .= getLecture($l,$course_id);
		}
	return $content."</ul>";
}

function getLecture($l, $course_id) {
	global $woodb;
	$materials = $woodb -> getMaterials($l->lecture_id);
	$lc = "<li><div>";
	$lc .= "<div class='woo_date' id = '".date("d/m/Y", strtotime($l->date))."'>".date("F j, Y \a\\t g:i a", strtotime($l->date))."</div>";
	
	global $wp_query;
	$page_link = '?page_id='.$wp_query->post->ID;
	$course_link = $page_link."&course_id=".$course_id;
	$lecture_link = $course_link."&lecture_id=".$l->lecture_id;
	$lc .= "<div class='woo_lecture_name'><a href='$lecture_link'>$l->name</a></div>";
	
	$lc .= "<div>".stripslashes($l->description)."</div>";
	
	if (isset($materials)) {
		$lc .= "<ul>";
		foreach ($materials as $m) {
			$image = "<img src='".get_bloginfo('wpurl') . "/wp-content/plugins/woodle/css/images/$m->type.png"."'/>";
			$lc .= "<li>$image <a href='$m->guid'>$m->name</a></li>";
		}
		$lc .= "</ul>";
	}
	
	return $lc."</div></li>";
}
?>